//
//  MeetingFromViewController.m
//  Gxj
//
//  Created by 刘伟 on 2017/8/29.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "MeetingFromViewController.h"
#import "MeetingScrollView.h"
#import "MeetingTrailerFromTableViewCell.h"
#import "WonderfulMeetingFromTableViewCell.h"
#import "ForecastMeetingViewController.h"
#import "HighlightReplaysTableViewCell.h"

@interface MeetingFromViewController ()<UITableViewDelegate,UITableViewDataSource,MeetingTrailerFromCellDelegate>
{
    MeetingScrollView    *_meetingScroll;
    NSMutableDictionary  *_dic;
    UIView               *_meetingHeadView;
}
@property (nonatomic,strong) UITableView *tableView;

//导航条上拉隐藏下拉显示
@property (nonatomic, assign) CGFloat borderNum;

@end

@implementation MeetingFromViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //去掉导航栏底部的黑线
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigation];
    
    [self setUpCycleScrollView];
    
    [self showMeetingView];
    
    _meetingHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, kHeightScale(160))];
    
    self.tableView.tableHeaderView = _meetingScroll;
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;// 去掉所有cell的分割线
    
    _dic = [NSMutableDictionary dictionary];
    
    [self.view addSubview:self.tableView];
}
#pragma mark - 获取会议预告
- (void)getMeetingBanner
{
    NSDictionary *dict = @{@"":@""};;
}

- (void)getMeetingForetell
{
    NSString *userid = [KQUserDefaults readUserDataWithKey:@"userid"];
    
    NSDictionary *dict = @{@"userid":userid,
                           @"status":@"1",
                           @"page":@"1",
                           @"index":@"2"
                           };
    
    [KQNetWork postWithURLString:[NSString stringWithFormat:@"%@/api/meeting/index",MAIN_URL] parameters:dict success:^(NSDictionary *dic) {
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 轮播图
- (void)setUpCycleScrollView {
    //轮播图的位置显示
    _meetingScroll = [[MeetingScrollView alloc ] initWithFrame:CGRectMake(0, 0, UIScreenWidth, kHeightScale(157))];
    _borderNum = 15.f;
    //Blick传值
    __block typeof(self) weakSelf = self;
    _meetingScroll.searchPushBlock = ^(PYSearchViewController *vc) {
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    
    _meetingScroll.searchPopBlock = ^(PYSearchViewController *vc) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    };
    
    _meetingScroll.newsPushBlock = ^(BOOL isSelect) {
        [weakSelf.navigationController pushViewController:[NewsFromViewController new] animated:YES];
    };
    
    _meetingScroll.classificationPushBlock = ^(BOOL isSelect) {
        [weakSelf.navigationController pushViewController:[ClassiFicationFromViewController new] animated:YES];
    };
}


#pragma mark - 头部滚动视图
- (void)showMeetingView
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    static float lastOffY  = 0;
    float curOffY = scrollView.contentOffset.y;
    
    if (scrollView.frame.size.height >= scrollView.contentSize.height ||     //内容高度低于scrollView高度，不隐藏
        fabs(curOffY)  +UIScreenWidth> scrollView.contentSize.height || //拉至最底部时，不做处理
        curOffY < 0                                                          //拉至最顶部时，不做处理
        )
    {
        return;
    }
    if (curOffY - _meetingScroll.navigationView.origin.y > 10)
    {
        //向上
        lastOffY = curOffY;
        [self.view addSubview:_meetingScroll.navigationView];
        _meetingScroll.navImage.image = [UIImage imageNamed:@"tbg"];
        _meetingScroll.navigationView.alpha = (curOffY -_borderNum )/_borderNum;
    }
    else if(UIScreenHeight -curOffY > UIScreenHeight - 90)
    {
        //向下
        lastOffY = curOffY;
        [_meetingScroll addSubview:_meetingScroll.navigationView];
        _meetingScroll.navImage.image = [UIImage imageNamed:@""];
        _meetingScroll.navigationView.alpha = (_borderNum -curOffY )/_borderNum;
    }
}

- (void)showTabBar
{
    if (self.tabBarController.tabBar.hidden == NO)
    {
        return;
    }
    
    UIView *contentView;
    if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]])
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    
    contentView.frame = CGRectMake(contentView.bounds.origin.x,
                                   contentView.bounds.origin.y,
                                   contentView.bounds.size.width,
                                   contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height);
    
    CATransition *animation = [CATransition animation];
    animation.duration = 0.4f;
    animation.type = kCATransitionMoveIn;
    animation.subtype = kCATransitionFromTop;
    self.tabBarController.tabBar.hidden = NO;
    [self.tabBarController.tabBar.layer addAnimation:animation forKey:@"animation2"];
    
    CATransition *animation1 = [CATransition animation];
    animation1.duration = 0.4f;
    animation1.type = kCATransitionMoveIn;
    animation1.subtype = kCATransitionFromBottom;
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar.layer addAnimation:animation1 forKey:@"animation3"];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg"]forBarMetrics:UIBarMetricsDefault];
}

- (void)hideTabBar
{
    if (self.tabBarController.tabBar.hidden == YES)
    {
        return;
    }
    UIView *contentView;
    if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] )
        contentView = [self.tabBarController.view.subviews objectAtIndex:1];
    else
        contentView = [self.tabBarController.view.subviews objectAtIndex:0];
    contentView.frame = CGRectMake(contentView.bounds.origin.x,
                                   contentView.bounds.origin.y,
                                   contentView.bounds.size.width,
                                   contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height);
    
    
    CATransition *animation1 = [CATransition animation];
    animation1.timingFunction=UIViewAnimationCurveEaseInOut;
    animation1.duration = 0.4f;
    //    animation1.delegate = self;
    animation1.type = kCATransitionReveal;
    animation1.subtype = kCATransitionFromTop;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar.layer addAnimation:animation1 forKey:@"animation0"];
    
    //定义个转场动画
    CATransition *animation = [CATransition animation];
    //转场动画持续时间
    animation.duration = 0.4f;
    //计时函数，从头到尾的流畅度？？？
    animation.timingFunction=UIViewAnimationCurveEaseInOut;
    //转场动画类型
    animation.type = kCATransitionReveal;
    //转场动画子类型
    animation.subtype = kCATransitionFromBottom;
    //动画时你需要的实现
    self.tabBarController.tabBar.hidden = YES;
    //添加动画 （转场动画是添加在层上的动画）
    [self.tabBarController.tabBar.layer addAnimation:animation forKey:@"animation1"];
}


#pragma mark - tableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger inter;
    if (section == 0) {
        inter = 1;
    }else if (section == 1){
        inter = 2;
    }else{
        inter = 2;
    }
    return inter;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat fffloat;
    if (indexPath.section == 0) {
        fffloat = kHeightScale(192);
    }else if (indexPath.section == 1){
        if (indexPath.section == 1 && indexPath.row == 0) {
            fffloat = kHeightScale(162);
        }else{
            fffloat = kHeightScale(171);
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            fffloat = kHeightScale(162);
        }else{
            fffloat = kHeightScale(171);
        }
    }
    return fffloat;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat ffloat;
    
    if (section == 2) {
        ffloat = kHeightScale(36);
    }else{
        ffloat = kHeightScale(36);
    }
    return ffloat;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [UIView new];
    
    NSArray* arr = @[@"预告会议",@"会议进行时",@"精彩回放"];
    
    UIImageView *leftImg = [[UIImageView alloc] init];
    leftImg.image = [UIImage imageNamed:@"fanhui"];
    // 填充
    leftImg.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:leftImg];
    
    UILabel * lab2 = [[UILabel alloc]init];
    lab2.textAlignment = 0;
    lab2.font = [UIFont boldSystemFontOfSize:kWidthScale(15)];
    lab2.textColor = [UIColor colorWithHexString:@"#333333"];
    lab2.text = arr[section];
    [view addSubview:lab2];
    
    lab2.sd_layout
    .centerYEqualToView(view)
    .leftSpaceToView(view, kWidthScale(15))
    .widthIs(kWidthScale(100))
    .autoHeightRatio(0);
    
    leftImg.sd_layout
    .centerYEqualToView(lab2)
    .rightSpaceToView(view,kWidthScale(15))
    .widthIs(kWidthScale(15))
    .heightIs(kHeightScale(15));
    
    view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    
    //创建一个手势进行点击，这里可以换成button
    
    UIButton * but = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, kWidthScale(36))];
    but.tag = 300+section;
    but.backgroundColor = [UIColor clearColor];
    [but addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:but];
    
    return view;
}

-(void)click:(UIButton *)sender
{
    UIButton *btn = (UIButton *)sender;
    
    switch (btn.tag) {
        case 300:
            [self.navigationController pushViewController:[ForecastMeetingViewController new] animated:YES];
            break;
            
        case 301:
            [self.navigationController pushViewController:[MeetingTheStormViewController new] animated:YES];
            break;
            
        case 302:
            [self.navigationController pushViewController:[MeetingTheStormViewController new] animated:YES];
            break;
        default:
            break;
    }
}


#pragma mark - tableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        MeetingTrailerFromTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"meeting"];
        cell.delegate = self;
        if (!cell) {
            cell = [[MeetingTrailerFromTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"meeting"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        __block typeof(self) weakSelf = self;
        cell.clickTrailerMeeting = ^(NSString *isClick) {
            [weakSelf.navigationController pushViewController:[DetailsOfTheMeetingFromViewController new] animated:YES];
        };
        return cell;
    }else if (indexPath.section == 1){
        WonderfulMeetingFromTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wonderful"];
        if (!cell) {
            cell = [[WonderfulMeetingFromTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"wonderful"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        __block typeof(self) weakSelf = self;
        cell.clickMeeting = ^(NSString *isClick) {
            [weakSelf.navigationController pushViewController:[DetailsOfTheMeetingFromViewController new] animated:YES];
        };
        return cell;
    }else if (indexPath.section == 2){
        HighlightReplaysTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HighlightReplays"];
        if (!cell) {
            cell = [[HighlightReplaysTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"HighlightReplays"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        __block typeof(self) weakSelf = self;
        cell.clickMeetingPlayback = ^(NSString *isClick) {
            [weakSelf.navigationController pushViewController:[DetailsOfTheMeetingFromViewController new] animated:YES];
        };
        return cell;
    }
    return nil;
}

- (void)clickTouchGrab:(UITapGestureRecognizer *)gesture
{
    [self.navigationController pushViewController:[DetailsOfTheMeetingFromViewController new] animated:YES];
}

#pragma mark - 设置导航
- (void)initNavigation{
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationItem.title = self.tabBarItem.title;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, UIScreenWidth, UIScreenHeight+kHeightScale(5)) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.showsVerticalScrollIndicator = NO;
//        _tableView.showsHorizontalScrollIndicator = NO;
    }
    return _tableView;
}

@end
